<?php

/*************************************************************************

	FILE INCLUDE

**************************************************************************/
include_once '../lib/Template.class.php';
$debug_mode_value = 0;



/*************************************************************************

	USER SCRIPT

***************************************************************************/
$userscript =<<<js
    <script type="text/javascript">
        <!--

        //-->
    </script>
js;


/*************************************************************************

		PAGE MAIN PROCESS

***************************************************************************/
$db->conn();

$query="update
		  candidate_tb inner join cluster_tb
		  on candidate_tb.cluster_cd  = cluster_tb.cluster_cd
		set candidate_tb.CANDIDATE_ST = 7,
		  cluster_tb.CLUSTER_ST = 7
		where cluster_tb.MERGING_ST = 0
		and cluster_tb.CLUSTER_ST = 6;";
$db->query($query);

$query="update candidate_tb
		 inner join (select  distinct a.cluster_cd
		            from candidate_tb a
		              inner join (select distinct rd_title from wiki_tb) b
		              on a.CANDIDATE_NM = b.rd_title
		            where a.REPRESENT_FL =0) b
		  on candidate_tb.cluster_cd = b.cluster_cd
		SET candidate_tb.REPRESENT_FL = 0
		where candidate_tb.CANDIDATE_ST = 7;";
$db->query($query);

$query="update candidate_tb
		 inner join (select a.CANDIDATE_CD
		            from candidate_tb a
		              inner join (select distinct rd_title from wiki_tb) b
		              on a.CANDIDATE_NM = b.rd_title
		            where a.REPRESENT_FL =0) b
		  on candidate_tb.CANDIDATE_CD = b.CANDIDATE_CD
		SET candidate_tb.REPRESENT_FL = 1
		where candidate_tb.CANDIDATE_ST = 7;";
$db->query($query);

$query="update candidate_tb a
		inner join (
		          select CLUSTER_CD,min(candidate_cd) as cndidate_cd from candidate_tb
		          where REPRESENT_FL = 1
		          group by CLUSTER_CD
		          having count(CLUSTER_CD) > 1 ) b
		on a.CLUSTER_CD =b.CLUSTER_CD
		and a.REPRESENT_FL = 1
		and a.CANDIDATE_CD <> b.cndidate_cd
		set a.REPRESENT_FL = 0;";
$db->query($query);

$query="update candidate_tb aa
		inner join
		(select distinct a.cluster_cd #a.*, b.CANDIDATE_CD,b.CANDIDATE_NM, c.page_title,c.rd_title
		      from candidate_tb a
		      inner join candidate_tb b
		      on a.CLUSTER_CD = b.CLUSTER_CD
		      and b.REPRESENT_FL =1
		        inner join wiki_tb c
		        on a.CANDIDATE_NM = c.page_title
		      where b.CANDIDATE_NM <> c.rd_title) bb
		on  aa.CLUSTER_CD = bb.CLUSTER_CD
		set aa.REPRESENT_FL = 0
		where aa.CANDIDATE_ST = 7;";
$db->query($query);

$query="INSERT INTO candidate_tb
		(CLUSTER_CD, CANDIDATE_NM, CANDIDATE_ST, REPRESENT_FL)
		select distinct a.cluster_cd,c.rd_title, a.CANDIDATE_ST,1
		      from candidate_tb a
		      left outer join candidate_tb b
		      on a.CLUSTER_CD = b.CLUSTER_CD
		      and b.REPRESENT_FL =1
		        inner join wiki_tb c
		        on a.CANDIDATE_NM = c.page_title
		where  b.CANDIDATE_NM is null and c.page_title is not null
		and a.CANDIDATE_ST = 7;";
$db->query($query);

$query="update cluster_tb aa
		inner join (
		        select distinct a.cluster_cd, b.min_cluster_cd  from candidate_tb a
		        inner join (select candidate_nm,min(cluster_cd) as min_cluster_cd  from candidate_tb
		                    group by candidate_nm
		                    having count(candidate_nm) > 1 ) b
		        on a.CANDIDATE_NM = b.CANDIDATE_NM ) bb
		on aa.cluster_cd = bb.cluster_cd
		set  MERGING_ST = 1,
		MERGING_CLUSTER =  bb.min_cluster_cd,
		CLUSTER_ST = 3;";
$db->query($query);

$query="update cluster_tb
		set CLUSTER_ST = 8
		where CLUSTER_ST = 7 ;";
$db->query($query);

$query="update candidate_tb a
		inner join cluster_tb b
		on a.cluster_cd = b.cluster_cd
		set a.candidate_st =b.CLUSTER_ST;";
$db->query($query);

$query="delete candidate_tb
		from candidate_tb
		inner join wiki_tb
		on candidate_tb.candidate_nm = wiki_tb.page_title
		where REPRESENT_FL = 0;";
$db->query($query);

$query="update cluster_tb a
		inner join (
		select distinct CLUSTER_CD from candidate_tb
		where CANDIDATE_ST = 8
		group by CLUSTER_CD
		having count(CLUSTER_CD) = 1) b
		on a.cluster_cd = b.cluster_cd
		set a.CLUSTER_ST = 1;";
$db->query($query);

$query=" update cluster_tb a
		inner join (
	  		select CLUSTER_CD from candidate_tb
			where CANDIDATE_ST =8
			group by CLUSTER_CD
			having max(REPRESENT_FL) =0) b
		on a.cluster_cd = b.cluster_cd
		set a.CLUSTER_ST = 3;";
$db->query($query);

$query="update candidate_tb a
		inner join cluster_tb b
		on a.cluster_cd = b.cluster_cd
		set a.candidate_st =b.CLUSTER_ST;";
$db->query($query);

$db->close();


/*************************************************************************

	TEMPLATE PROCESS

***************************************************************************/
$JSClass->replace("./SynonymSearch.php");





/*************************************************************************

	AJAX FUNCTION : Template_ function

***************************************************************************/



















?>
